#include<iostream>
using namespace std;

#include<string>
#include<vector>
#include<list>

//int main()
//{
//	int x = 0, y = 1;
//	swap(x, y);
//
//	double m = 1.1, n = 2.2;
//	swap(m, n);
//
//
//  cout<<m<<"  "<<n<<endl;
//  cout<<x<<"  "<<y<<endl;
//	return 0;
//}


//模板+函数
//模板+类 
//
//template<class T>
//
//class Stack
//{
//  public:
//    void push(const T& x)
//    {
//      //...
//    }
//
//  private:
//    T* _a;
//    int _top;
//    int _capcity;
//};
//
//
//template<class T>
//void Stack<T>::push(const T& x)
//{
//
//}
//
//
//int main()
//{
//  //同一个类模板，不同类型
//  Stack<int> s1;
//  Stack<double> s2;
//  
//  return 0;
//}
//
//


void test_string1()
{
  string s0;
  string s1("hello world");
  string s2(s1);
  string s3(s1,5,3);
  string s4(s1,5,10);
  string s5(s1,5);


  cout<<s0<<endl;
  cout<<s1<<endl;
  cout<<s2<<endl;
  cout<<s3<<endl;
  cout<<s4<<endl;
  cout<<s5<<endl;

  string s6(10,'#');
  cout<<s6<<endl;
}

void test_string2()
{
  string s1("hello world");

  for(size_t i=0;i< s1.size();i++)
  {
    cout<<s1[i]<<" ";
    cout<<s1.operator[](i)<<endl;
  }
  cout<<endl;

  string s3("hello world");
  s3[0]++;
  cout<<s3<<endl;

  const string s2("hello world");
  cout<<s2<<endl;

  cout<<s3.size()<<endl;
  cout<<s3.capacity()<<endl;
   
  string::iterator it3 = s3.begin();
  while(it3 != s3.end())
  {
    cout<<*it3<<" ";
    ++it3;
  }
  cout<<endl;

 // cout<< typeid(it3).name() <<endl;
 //
 //
/// for(auto e : s3)
/// {
///   cout<<e<<" ";
/// }
/// cout<<endl;
///
/// }
///

}
void test_string3()
{
  string s1("hello world");
  string::reverse_iterator  rit = s1.rbegin();//反向迭代器
  while(rit != s1.rend())
  {
    cout<<*rit<<" ";
    ++rit;
  }
  cout<<endl;


  
  const  string s3("hello world");
  string::const_iterator  it3 = s3.begin();//反向迭代器
  while(it3 != s3.end())
  {
    cout<<*it3<<" ";
    ++it3;
  }
  cout<<endl;
}

void test_string4()
{
    string s1("hello world");
    cout<<s1.size()<<endl;
    cout<<s1.length()<<endl;
    cout<<s1.max_size()<<endl;
    cout<<s1.capacity()<<endl;
    

    string s;
    size_t sz=s.capacity();
    cout<<"capacity changed"<<sz<<'\n';

    cout<<"making s grow:\n";
    for(int i=0;i<100;i++)
    {
      s.push_back('c');
      if(sz!=s.capacity())
      {
        sz=s.capacity();
        cout<<"capcity changed: "<<sz<<'\n';
      }
    }

    s1.clear();
    cout<<s1<<endl;
    cout<<s1.capacity()<<endl;
    cout<<s1.size()<<endl;


//    s1.shrink_to_fit();//缩容
   // cout<<s1.capacity()<<endl;
   // cout<<s1.size()<<endl;
   //
   //
}


void test_string5()
{
  string s1("hello worldxxxx");
  cout<<s1.size()<<endl;
  cout<<s1.capacity()<<endl;


  s1.reserve(500);//reserve比capcity大的时候才会起作用
 // resize  比size小会删除
 // 在size和capcity之间会插入
 // 比size大会 扩容+插入
 
  cout<<s1.size()<<endl;
  cout<<s1.capacity()<<endl;
  cout<<"xxxxxxxxxxxxxxxxxxxxxxxxxxxx"<<endl;
  

  string s2("hello worldxxxxx");
  cout<<s2.size()<<endl;
cout<<s2.capacity()<<endl<<endl;

  s2.resize(40);


  cout<<s2.size()<<endl;
  cout<<s2.capacity()<<endl<<endl;
}

void test_string6()
{
  string  s1("helluo world");
  cout<<s1[5]<<endl<<endl;

  cout<<s1.at(5)<<endl;

  try 
  {
  s1.at(15);
 // s1[15];

  }

  catch(const exception &e)
  {
    cout<<e.what()<<endl;
  }
}

void test_string7()
{
  string s1("hello world");
  s1.push_back('!');
  cout<<s1<<endl;

  s1.append("hello bit");

  cout<<s1<<endl;

  s1.append(10,'z');
  cout<<s1<<endl;
  
  string s2("  apple ");

  s1.append(++s2.begin(),--s2.end());//迭代区间指向的位置
  //第一个位置的下一个位置开始，最后一个位置的前一个位置开始
  cout<<s1<<endl;
  string s3("hello world");
  s3+=' ';//+=就是直接往后面加
  s3+="apple";
  cout<<s3<<endl;
}

void test_string8()
{
  string s1("xhello world");
  cout<<s1<<endl;

  s1.assign("xxxx");//assign是用一个值去覆盖
  cout<<s1<<endl;

  s1.insert(0,"yyyy");//插入
  cout<<s1<<endl;

  s1.erase(5,3);//从第几个位置开始，删除几个
  cout<<s1<<endl;

  s1.erase();
  cout<<s1<<endl;


  string s2("hello world hello zyy");
  //s2.replace(5,1,"%20");
 // cout<<s2<<endl;

  size_t pos =s2.find(' ');
  while(pos != string::npos)
  {
      s2.replace(pos,1,"%20");//pos位置的一个字符 替换成20%
      pos = s2.find(' ');
  }
  cout<<s2<<endl;
  
  //string s3;
  //s3.reserve(s2.size());//开空间
  //for(auto ch : s2)
  //{
  //  if(ch != ' ')
  //  {
  //    s3 += ch;
  //  }
  //  else{
  //    s3 += "20%";
  //  }
  //}
  //cout<<s3<<endl;
  //s2.swap(s3);
  //cout<<s2<<endl;
}


void test()
{
    string s1;
    string s2("hello world");
    string s3(s2);

    cout<<s1<<endl;
    cout<<s2<<endl;
    cout<<s3<<endl;
}


void test1()
{
  string s("hello world");

  s.resize(s.size()+1,'+');
  cout<<s<<endl;
}

void test2()
{
  string s("hello world");

  int sz=s.size();
  cout<< s.size()<<endl;


  cout<< s.length()<<endl;
  cout<< s.capacity()<<endl;
  cout<< s.empty()<<endl;
  s.resize(sz+2,'*');
  cout<<s<<endl<<endl;

  s.reserve();
}

void test3()
{
  string s;
  s.reserve(100);
  cout<<s.size()<<endl;
  cout<<s.capacity()<<endl;


  s.reserve(50);
  cout<<s.size()<<endl;
  cout<<s.capacity()<<endl;
}


void test4()
{
  string s("hello zyy");

  //for+operator[]
  for(size_t i=0;i<s.size();i++)
  {
    cout<<s[i]<<endl;
  }


  //2.迭代器
  string::iterator zz=s.begin();
  while(zz != s.end())
  {
    cout<<*zz<<endl;
    ++zz;
  }
    

  //使用auto迭代器
//  auto  rit = s.rbegin();
//  while(rit !=s.rend())
//  {
//    cout<<*rit<<endl;
//  }
//

//使用范围for

  for(auto  ch : s)
  {
    cout<<ch<<endl;
  }
}

void test5()
{
  string str;
  str.push_back(' ');
  str.append("hello");

  str+='z';
  str+="yy";
  cout<<str<<endl;
  cout<<str.c_str()<<endl<<endl;

//获取file的后缀
 string file("string.cpp");
 size_t pos = file.rfind('.');//rfind从后往前找

 string suffix(file.substr(pos,file.size()-pos));
 cout<<suffix<<endl;
    
 string url("http://www.cplusplus.com/reference/string/string/find/");
	cout << url << endl;
	size_t start = url.find("://");
	if (start == string::npos)
	{
		cout << "invalid url" << endl;
		return;
	}
	start += 3;
  start+=3;
	size_t finish = url.find('/', start);
	string address = url.substr(start, finish - start);
	cout << address << endl;


}


//字符数组

struct Person
{
  private :
    int _age;
    char name[36];
    char _tel[36];
};

void test6()
{
  string s1("hello world");


  string  filename("test.cpp");
  FILE* fout=fopen(filename.c_str(),"r");


  string url1("https://legacy.cplusplus.com/reference/cmath/ceil/?kw=ceil");
  string url2("https://www.nowcoder.com/practice/9b969a3ec20149e3b870b256ad40844e?tpId=230&tqId=39751&ru=/exam/oj");

  string protocol,domain,uri;
  size_t i1=url1.find(':');

  if(i1 != string::npos)
  {
      protocol = url1.substr(0,i1-0);//从某个位置开始取n个字符
      cout<<protocol<<endl;
  }

  
  size_t i2=url1.find('/',i1+3);

  if(i2 != string::npos)
  {
      domain = url1.substr(i1+3,i2-(i1+3));//从某个位置开始取n个字符
      cout<<domain<<endl;
  }
  size_t i3 =  url1.find("cplusplus");
  cout<<i3<<endl;

  cout<<(url1<url2)<<endl;
}

void test7()
{
    string s1="zzyy";
    string s2="123";
    string ret=s1+s2;
    cout<<ret<<endl;
}

void test8()//tostring
{
  int i=1234;
  double d=2.455;

  string s1 = to_string(i);
  string s2 = to_string(d);

  cout<<s1<<endl<<s2<<endl;
}


int main()
{
  test8();
  return 0;
}









